package com.niit.controller;

import com.google.gson.JsonObject;
import com.niit.pojo.Orders;
import com.niit.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/orders")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    // 添加订单接口
    @RequestMapping("/add")
    public int addOrder(Orders order) {
        JsonObject jsonObject = new JsonObject();
        boolean result = ordersService.addOrder(order);
        if (result) {
            jsonObject.addProperty("code", 1);
            return 1;
        }
        return 0;
    }

    // 根据订单ID删除订单接口
    @RequestMapping("/delete")
    public int deleteOrder(int oid) {
        JsonObject jsonObject = new JsonObject();
        boolean result = ordersService.deleteOrderById(oid);
        if (result) {
            jsonObject.addProperty("code", 1);
            return 1;
        }
        return 0;
    }

    // 根据订单ID查询订单详情接口
    @RequestMapping("/detail")
    public Orders getOrderDetail(int oid) {
        return ordersService.getOrderById(oid);
    }

    // 更新订单接口
    @RequestMapping("/update")
    public int updateOrder(Orders order) {
        JsonObject jsonObject = new JsonObject();
        boolean result = ordersService.updateOrder(order);
        if (result) {
            jsonObject.addProperty("code", 1);
            return 1;
        }
        return 0;
    }

    // 获取所有订单列表接口
    @RequestMapping("/list")
    public List<Orders> getOrderList() {
        return ordersService.getAllOrders();
    }

    // 根据用户ID获取该用户的所有订单列表接口
    @RequestMapping("/userOrders")
    public List<Orders> getOrdersByUserId(int uid) {
        return ordersService.getOrdersByUserId(uid);
    }
}
